<?php
	session_start();
	require_once('common/config.php');
	require_once('../_helper/paging.php');
	require_once('../_helper/helper.php');
	require_once('../_model/product_model.php');
	require_once('../_model/brand_model.php');
	require_once('../_model/type_model.php');
	require_once('../_model/cat_model.php');
	
	$pageCMS = PAGE_CMS_PRODUCT;
	$keyword = '';
	$type_id = '';
	$brand_id = '';
	
	if(!isset($_SESSION['logon']))
	{
		header("Location: /wks/login.php?url=product");
		exit();
	}
	
	if (!isset($_GET['action']))
		$_GET['action'] = "list";
		
	if (isset($_GET['id'])){
		$product_id = $_GET['id'];
		}
	if (isset($_GET['keyword'])){
		$keyword = $_GET['keyword'];
	}
	if (isset($_GET['brand_id'])){
		$brand_id = $_GET['brand_id'];
	}
	if (isset($_GET['type_id'])){
		$type_id = $_GET['type_id'];
	}
	
	$action = $_GET['action'];
	
	if($action == 'list' || $action =='search')
	{
		if(isset($_GET['page']))
			$curPage = $_GET['page'];
		else
		{
			$curPage = 0;
			unset($_SESSION['numProduct']);
		}
			
		
		/*var_dump($type_arr);die();*/
		if( !isset($_SESSION['numProduct']))
		{
			$numProduct = GetNumProductSearch($type_id, $brand_id,$keyword);
			$_SESSION['numProduct'] = $numProduct;
		}
		else
			$numProduct = $_SESSION['numProduct'];
			
		$param = "action=search&brand_id={$brand_id}&keyword={$keyword}&type_id={$type_id}";	
		$brand_arr = GetBrandAll();
		$type_arr = GetTypeList();
		$cat_arr = GetCatList();
		$product_arr = GetProductSearch($curPage, PAGING_NUM_ROW , $type_id, $brand_id,$keyword);
		
				
		//Xu ly
		require_once('template/product/product_list.php');
		exit();
	}
	
	if($action == 'add')
	{
		if(!isset($_POST['submit'])){
			
			$brand_arr = GetBrandAll();
			$type_arr = GetTypeList();
			$cat_arr = GetCatList();
			require_once('template/product/product_add.php');
			exit();
		}
		else
		{
			
			if($_POST['product_name'] != '' && $_POST['brand_id'] != '' && $_POST['type_id']!='')
			{
				//$time = time();
				//$subDir = $time%20;
				//$img_url = Upload($_FILES, $time.'-', "/images/product/".$subDir.'/');
				//if($img_url == null)
					//$img_url = "../images/product/product_default.png";
				
				
				$order   = array("\r\n", "\n", "\r", "\t", "  ","<p>&nbsp;</p>");
				$_POST['product_des_short'] = str_replace($order,'',$_POST['product_des_short']);
				$_POST['product_des_full'] = str_replace($order,'',$_POST['product_des_full']);
					
				$_POST['product_des_short'] = strip_tags($_POST['product_des_short'],'<p><strong>');
				
				$result = InsertProduct($_POST);
				if($result)
				{
					$url = urlencode(BASE_URL. "/sitemap.xml");
					$url = 'http://www.google.com/webmasters/tools/ping?sitemap='.$url;
					//GetContent($url);
					InsertBrandType($_POST);
					header("Location: /wks/product.php?action=list");
				}
				else
					header("Location: /wks/product.php?action=list&error=1&e_action=add");
			}
			else
				header("Location: /wks/product.php?action=list&error=1&e_action=add");
		}
		exit();
	}
	
	if($action == 'edit')
	{
		if (!isset($product_id))
			header("Location: /wks/product.php");		
			
		if(!isset($_POST['submit'])){
			$product_value = GetProductById($product_id);
			$brand_arr = GetBrandAll();
			$type_arr = GetTypeList();
			$cat_arr = GetCatList();
			require_once('template/product/product_edit.php');
			exit();
		}
		else{
			if($_POST['product_name'] != '' && $_POST['brand_id'] != '' && $_POST['type_id']!='')
			{
				
				// $time = time();
				// $subDir = $time%20;
				// $img_url = Upload($_FILES, $time.'-', "/images/product/".$subDir.'/');
								
				$order   = array("\r\n", "\n", "\r", "\t", "  ","<p>&nbsp;</p>");
				$_POST['product_des_short'] = str_replace($order,'',$_POST['product_des_short']);
				$_POST['product_des_full'] = str_replace($order,'',$_POST['product_des_full']);
				
				$_POST['product_des_short'] = strip_tags($_POST['product_des_short'],'<p><strong>');
				
				EditProduct($product_id, $_POST);
				header("Location: /wks/product.php?action=list");
			}
		}
		exit();
	}
	
	if($action == 'del')
	{
		$result = FALSE;
		if (isset($product_id))
			$result = DeleteProduct($product_id);
			
		if($result == FALSE)
			header("Location: /wks/product.php?error=1&e_action=del");	
		else
		{
			header("Location: /wks/product.php");	
		}
	}
	if($action == 'adjorder')
	{
		//var_dump($_GET); die();
		$product_id = $_GET['id'];
		
		if($_GET['adj'] == 'inc')
		{
			if (UpdateProductOrder($product_id, '+') )
				echo "ok";
			else
				echo "fail";
		}
		if($_GET['adj'] == 'desc')
		{
			if (UpdateProductOrder($product_id, '-') )
				echo "ok";
			else
				echo "fail";
		}
		exit();
	}
	if($action == 'adjhot')
	{
		$product_id = $_GET['id'];
		
		if($_GET['adj'] == 'hot')
		{
			if (UpdateProductHot($product_id, 'hot') )
				echo "ok";
			else
				echo "fail";
		}
		if($_GET['adj'] == 'normal')
		{
			if (UpdateProductHot($product_id, 'normal') )
				echo "ok";
			else
				echo "fail";
		}
		exit();
	}
	
	if($action == 'palette')
	{
		$product_id = $_GET['id'];
		$palette = $_GET['url'];
		
		if (UpdatePalette($product_id, $palette) )
				echo $palette;
			else
				echo "error";
		
		exit();
	}
?>